package com.example.shaidemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrowseActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {
    private static final String TAG = "BrowseActivity";
    ListView listView;
    SimpleAdapter simpleAdapter;
    List<Map<String,String>> browseList;
    TextView deleteAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse);

        //列表绑定设置
        listView = findViewById(R.id.browse_list);
        browseList = GetData.getData();
        simpleAdapter = new SimpleAdapter(this,browseList,
                R.layout.browse_list_view,
                new String[]{"title","company","majority","salary"},
                new int[]{R.id.title,R.id.company,R.id.majority,R.id.salary});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemLongClickListener(this::onItemLongClick);

        //清空记录
        deleteAll = findViewById(R.id.delete_all);
        deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(browseList.size() > 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(BrowseActivity.this);
                    builder.setTitle("提示").setMessage("请确认是否删除全部记录？").setPositiveButton("是",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            browseList.clear();
                            simpleAdapter.notifyDataSetChanged();
                        }
                    }).setNegativeButton("否",null);
                    builder.create().show();
                    Log.i(TAG, "onClick: 删除全部记录！");
                }
            }
        });
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        Log.i(TAG, "onItemLongClick: 长按列表项position=" + position);

        Object itemAtPosition = listView.getItemAtPosition(position);
        //删除操作
        //构造对话框进行确认操作
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前记录").setPositiveButton("是",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: 对话框事件处理");
                if(browseList.remove(position)!=null){
                    System.out.println("success");
                }else{
                    System.out.println("failed");
                }
                simpleAdapter.notifyDataSetChanged();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
        Log.i(TAG, "onItemLongClick: size=" + browseList.size());

        return true;
    }
}